#include "CMonsterConfig.h"
#include "../battle/CBattleData.h"
#include "../mainctrl.h"

int CMonsterGroupConfig::AddConfig(CMonsterGroupConfigData& stConfigData)
{
    int iRet = 0;
    int iIndex = _stMonsterGroupConfigHash.HashFind(stConfigData._uiMonsterGroupID);
    if (iIndex >= 0)
    {
        CT_ERROR(("add duplicate config , group id %u", stConfigData._uiMonsterGroupID));
        return RET_SYSTEM_FAIL;
    }

    T_BATTLE_SIDE_DATA aiMonsterPos;
    T_BATTLE_SIDE_DATA aiMonsterOrder;
    aiMonsterPos.Clear();
    aiMonsterOrder.Clear();
    for (int i = 0; i < stConfigData._astMonsters.Size(); ++i)
    {
        aiMonsterPos.AddOneItem(stConfigData._astMonsters[i]._usPos);
        aiMonsterOrder.AddOneItem(stConfigData._astMonsters[i]._ucTurnOrder);
    }

    iRet = CMainCtrl::m_pstMain->m_stMatrixMgr.CheckValidPos(aiMonsterPos);
    if (iRet)
    {
        CT_ERROR(("monster group %u pos valid", stConfigData._uiMonsterGroupID));
        return iRet;
    }
    iRet = CMainCtrl::m_pstMain->m_stMatrixMgr.CheckValidOrder(aiMonsterOrder);
    if (iRet)
    {
        CT_ERROR(("monster group %u order valid", stConfigData._uiMonsterGroupID));
        return iRet;
    }

    iIndex = _stMonsterGroupConfigHash.HashAlloc(stConfigData._uiMonsterGroupID);
    if (iIndex < 0)
    {
        CT_ERROR(("monster group hash is full, group id %u", stConfigData._uiMonsterGroupID));
        return RET_SYSTEM_FAIL;
    }

    _stMonsterGroupConfigHash[iIndex] = stConfigData;
    return 0;
}

